package com.wxn.learncompose.pagers

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.rememberScrollableState
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp

@Composable
fun TextClickPager() {
    var count by remember{ mutableStateOf(0) }

    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ){

        Column(modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally) {
            Text(
                "${count}",
                color = Color.Blue,
                textAlign = TextAlign.Center,

                fontSize = TextUnit(22f, TextUnitType.Sp),
                modifier = Modifier
                    .clickable {
                        count += 1
                    }
                    .size(100.dp)
                    .pointerInput(Unit) {  //点击事件
                        detectTapGestures(
                            onDoubleTap = {   //双击事件
                                Log.d("TextClickPager", "onDoubleTap")
                            },
                            onPress = {  //按下事件
                                Log.d("TextClickPager", "onPress")
                            },
                            onLongPress = {  //长按事件
                                Log.d("TextClickPager", "onLongPress")
                            },
                            onTap = {  //点击事件
                                Log.d("TextClickPager", "onTap")
                            })
                    }
            )

//            借助 ScrollState ，您可以更改滚动位置或获取当前状态。如需使用默认参数创建此列表，请使用 rememberScrollState() 。
            val scrollState = rememberScrollState()
            LaunchedEffect(scrollState) {
                scrollState.animateScrollTo(200) //控制滚动到给定位置
            }
            Column(modifier = Modifier
                .fillMaxWidth()
                .height(100.dp)
                .verticalScroll(scrollState),  //滚动修饰符
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally) {
                repeat(10) {
                    Text("$it item")
                }
            }

//            scrollable 修饰符与滚动修饰符不同，区别在于 scrollable 可检测滚动手势，但不会偏移其内容。
//            此修饰符需要 ScrollableController 才能正常运行。构造 ScrollableController 时，
//            您必须提供一个 consumeScrollDelta 函数，该函数将在每个滚动步骤（通过手势输入、平滑滚动或投掷）调用，
//            并且增量以像素为单位。为了确保正确传播事件，必须从此函数返回使用的滚动距离量。
            var offset by remember { mutableStateOf(0f) }
            Box(
                Modifier
                    .background(Color.LightGray)
                    .size(150.dp)
                    .scrollable(orientation = Orientation.Vertical,
                        state = rememberScrollableState {delta ->
                            offset += delta
                            delta
                        }),
                contentAlignment = Alignment.Center) {
                Text("${offset.toString()}")
            }
        }
    }
}